package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.constant.enums.CorFileTypeEnum;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.security.service.TdfUser;
import com.engine4cloud.tdf.common.security.util.SecurityUtils;
import com.engine4cloud.tdf.supervision.convert.RcglEducationLearningConvert;
import com.engine4cloud.tdf.supervision.dto.create.RcglEducationLearningCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxFileQueryDTO;
import com.engine4cloud.tdf.supervision.dto.query.RcglEducationLearningQusryDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.RcglEducationLearningResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.RcglEducationLearningUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.RcglEducationLearningEntity;
import com.engine4cloud.tdf.supervision.mapper.RcglEducationLearningMapper;
import com.engine4cloud.tdf.supervision.service.JzdxFileService;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import com.engine4cloud.tdf.supervision.service.RcglEducationLearningService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 教育学习
 *
 * @author wenjie
 * @date 2024-04-28 09:43:11
 */
@Service
public class RcglEducationLearningServiceImpl extends ServiceImpl<RcglEducationLearningMapper, RcglEducationLearningEntity> implements RcglEducationLearningService {
    @Resource
    private JzdxService jzdxService;

    @Resource
    private JzdxFileService fileService;

    @Override
    public PageInfo<RcglEducationLearningResultDTO> getPageList(Params pageable, RcglEducationLearningQusryDTO dto) {
        return Pages.convert(this.baseMapper.pageList(Pages.page(pageable), dto));
    }

    @Override
    public RcglEducationLearningResultDTO getById(Long id) {
        RcglEducationLearningResultDTO result = Optional.ofNullable(RcglEducationLearningConvert.INSTANCE.convert(super.getById(id)))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));
        //设置教育学习测试及小结
        this.setExaminationPaperList(result);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(RcglEducationLearningCreateDTO dto) {
        JzdxResultDTO dx = Optional.ofNullable(jzdxService.getByCode(dto.getRybh()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));

        TdfUser user = SecurityUtils.getUser();
        RcglEducationLearningEntity entity = RcglEducationLearningConvert.INSTANCE.create(dto, dx.getTenantId());
        //保存主体信息
        super.save(entity);
        //保存文件信息
        this.setExaminationPaperList(dto, entity, user);
        return true;
    }

    @Override
    public Boolean update(RcglEducationLearningUpdateDTO dto) {
        RcglEducationLearningEntity old = Optional.ofNullable(super.getById(dto.getId()))
                .orElseThrow(() -> new ValidateCodeException("数据不存在"));

        TdfUser user = SecurityUtils.getUser();
        RcglEducationLearningEntity entity = RcglEducationLearningConvert.INSTANCE.update(dto);
        //保存主体信息
        super.updateById(entity);
        //保存文件信息
        this.updateInstrumentList(dto, old, user);
        return true;
    }

    public void setExaminationPaperList(RcglEducationLearningCreateDTO dto, RcglEducationLearningEntity entity, TdfUser user) {
        fileService.batchSaveFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_EDUCATION_LEARNING), dto.getExaminationPaperList(), user);
    }

    public void updateInstrumentList(RcglEducationLearningUpdateDTO dto, RcglEducationLearningEntity entity, TdfUser user) {
        fileService.batchUpdateFile(new JzdxFileQueryDTO(entity.getRybh(), entity.getLcbh(), CorFileTypeEnum.CORRECTOR_EDUCATION_LEARNING), dto.getExaminationPaperList(), user);
    }

    private void setExaminationPaperList(RcglEducationLearningResultDTO dto) {
        JzdxFileQueryDTO query = new JzdxFileQueryDTO(dto.getRybh(), dto.getLcbh(), CorFileTypeEnum.CORRECTOR_EDUCATION_LEARNING);
        dto.setExaminationPaperList(fileService.findList(query));
    }
}